package com.xxhui.guidepagedemo;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;

import com.xxhui.guidepagedemo.Fragment.ScaleStackViewPageFragment;
import com.xxhui.guidepagedemo.Fragment.StackViewPageFragment;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity" ;
    private FragmentManager fragmentManager;

    private static FragmentTransaction fragmentTransaction;

    private ScaleStackViewPageFragment scaleStackViewPageFragment ;

    private StackViewPageFragment stackViewPageFragment ;

    private Fragment fragmentsArray[] = new Fragment[2] ;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportActionBar().hide();
        fragmentManager = getFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        stackViewPageFragment = (StackViewPageFragment) fragmentManager.findFragmentById(R.id.stack_viewpage_fragment);
        scaleStackViewPageFragment = new ScaleStackViewPageFragment();
        fragmentTransaction.add(R.id.contianer,scaleStackViewPageFragment).hide(scaleStackViewPageFragment).commit();//必须先加到contianer，不然找不到
        fragmentsArray[0] = stackViewPageFragment;
        fragmentsArray[1] = scaleStackViewPageFragment;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_MENU) {//让菜单点击事件能传递到上层处理
            getSupportActionBar().show();
            return false;
        }else if(keyCode == KeyEvent.ACTION_DOWN) {//让触屏事件点击能传递到上层处理
            return false;
        }else {
            getSupportActionBar().hide();
        }
        return true;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0,1,1,"StackViewPage");
        menu.add(0,2,2,"ScaleStackViewPage");
        return super.onCreateOptionsMenu(menu);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        fragmentTransaction = fragmentManager.beginTransaction();
        switch (item.getItemId()){
            case 1:
                //fragmentTransaction.hide(scaleStackViewPageFragment).show(stackViewPageFragment).commit();
                this.changeShow(stackViewPageFragment);
                break;
            case 2:
                //fragmentTransaction.hide(stackViewPageFragment).show(scaleStackViewPageFragment).commit();
                this.changeShow(scaleStackViewPageFragment);
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void changeShow(Fragment show){
        fragmentTransaction = fragmentManager.beginTransaction();
        for(int i=0 ; i<fragmentsArray.length ;i++){
            Log.i(TAG, "changeShow: "+i);
            fragmentTransaction = fragmentTransaction.hide(fragmentsArray[i]);
        }
        fragmentTransaction.show(show).commit();
    }
}
